#include "mainwindow.h"

#include "qstringlist.h"
#include "qdatetime.h"
#include "qrandom.h"
#include "qlabel.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindowClass())
{
    ui->setupUi(this);

    label_cell_index = new QLabel("当前单元坐标：", this);
    label_cell_index->setMidLineWidth(250);
    ui->statusBar->addWidget(label_cell_index);

    label_cell_type = new QLabel("类型：", this);
    label_cell_type->setMidLineWidth(200);
    ui->statusBar->addWidget(label_cell_type);

    label_student_id = new QLabel("学生学号：", this);
    label_student_id->setMidLineWidth(250);
    ui->statusBar->addWidget(label_student_id);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::creatIteamsARow(int rowNo, QString Name, QString sex, QDate birth, QString nation, bool isPM, int score)
{
    uint16_t studID = 20022100;
    QTableWidgetItem* item = nullptr;

    item = new QTableWidgetItem(Name, cell_type::name);
    item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    studID += rowNo;
    item->setData(Qt::UserRole, QVariant(studID));
    ui->tableWidget->setItem(rowNo, column_number::name, item);

    QIcon icon;
    if (sex == "男生")
        icon.addFile(":/MainWindow/image/男260X260");
    else
        icon.addFile(":/MainWindow/image/女260X260");
    item = new QTableWidgetItem(sex, cell_type::sex);
    item->setIcon(icon);
    item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
    item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    ui->tableWidget->setItem(rowNo, column_number::sex, item);

    item = new QTableWidgetItem(birth.toString("yyyy-MM-dd"), cell_type::birth);
    item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    ui->tableWidget->setItem(rowNo, column_number::birth, item);

    item = new QTableWidgetItem(nation, cell_type::nation);
    item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    ui->tableWidget->setItem(rowNo, column_number::nation, item);

    item = new QTableWidgetItem("党员", cell_type::party);
    item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled |Qt::ItemIsUserCheckable);//ItemIsUserCheckable允许选择
    item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    if (isPM)//复选框设置
        item->setCheckState(Qt::Checked);
    else
        item->setCheckState(Qt::Unchecked);
    item->setBackground(QBrush(Qt::yellow));//背景颜色
    ui->tableWidget->setItem(rowNo, column_number::party, item);

    item = new QTableWidgetItem(QString::number(score), cell_type::score);
    item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    ui->tableWidget->setItem(rowNo, column_number::score, item);
}

void MainWindow::on_pushButton_SetHeader_clicked()
{
    ui->tableWidget->clear();

    QStringList headerText;
    headerText << "姓名" << "性别" << "出生日期" << "民族" << "分数" << "是否党员";
    ui->tableWidget->setColumnCount(headerText.size());

    for (size_t i = 0; i < ui->tableWidget->columnCount(); i++)
    {
        //需要new出来放到堆区，局部变量释放时，对应的item也就没了，应该是底层析构写好了
        QTableWidgetItem* item=new QTableWidgetItem(headerText.at(i));
        QFont font = item->font();
        font.setBold(true);
        font.setPointSize(11);
        item->setFont(font);
        item->setForeground(QBrush(Qt::red));
        ui->tableWidget->setHorizontalHeaderItem(i, item);//不是简单的拷贝数据

        //以下不对
        //QTableWidgetItem item(headerText.at(i));
        ////...
        //ui->tableWidget->setHorizontalHeaderItem(i, &item);//不是简单的拷贝数据
    }
}

void MainWindow::on_pushButton_SetRows_clicked()
{
    ui->tableWidget->clearContents();//不删除表头，仅删除表格中数据区内所有单元格的内容
    ui->tableWidget->setRowCount(ui->spinBox->value());
}

void MainWindow::on_pushButton_InitData_clicked()
{
    ui->tableWidget->clearContents();
    QDate birth(2000, 2, 2);
    for (size_t i = 0; i < ui->tableWidget->rowCount(); i++)
    {
        QString strName = QString("学生%1").arg(i);
        QString strSex = (i % 2) ? "男生" : "女生";
        bool isParty = (i % 2) ? false : true;
        int score = QRandomGenerator::global()->bounded(60, 100);
        creatIteamsARow(i, strName, strSex, birth, "汉族", isParty, score);

    }
}

void MainWindow::on_pushButton_InsertRow_clicked()
{
    int curRow = ui->tableWidget->currentRow();
    ui->tableWidget->insertRow(curRow);
    creatIteamsARow(curRow, "新学生", "男生", QDate::fromString("2000-1-1", "yyyy-M-d"), "汉族", false, 66);
}

void MainWindow::on_pushButton_AppendRow_clicked()
{
    int rowCount = ui->tableWidget->rowCount();
    ui->tableWidget->insertRow(rowCount);
    creatIteamsARow(rowCount, "新学生", "女生", QDate::fromString("2000-1-1", "yyyy-M-d"), "汉族", false, 99);
}

void MainWindow::on_pushButton_DeleteCurrentRow_clicked()
{
    int curRow = ui->tableWidget->currentRow();
    ui->tableWidget->removeRow(curRow);
}

void MainWindow::on_pushButton_AutoHeight_clicked()
{
    ui->tableWidget->resizeRowsToContents();
}

void MainWindow::on_pushButton_AutoWidth_clicked()
{
    ui->tableWidget->resizeColumnsToContents();
}

void MainWindow::on_pushButton_ReadToEdit_clicked()
{
    ui->plainTextEdit->clear();
    QTableWidgetItem* item = nullptr;
    for (int i = 0; i < ui->tableWidget->rowCount(); i++)
    {
        QString str = QString::asprintf("第%d行：", i + 1);
        for (int j = 0; j < ui->tableWidget->columnCount() - 1; j++)
        {
            item = ui->tableWidget->item(i, j);
            str += item->text();
        }
        item = ui->tableWidget->item(i, column_number::party);
        if (item->checkState() == Qt::Checked)
            str += "党员";
        else
            str += "群众";

        ui->plainTextEdit->appendPlainText(str);
    }
}

void MainWindow::on_checkBox_Editable_clicked(bool checked)
{
    if (checked)
        ui->tableWidget->setEditTriggers(QAbstractItemView::DoubleClicked |
            QAbstractItemView::SelectedClicked);//双击或选中时可以编辑
    else
        ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);//不可编辑
}

void MainWindow::on_checkBox_RowColors_clicked(bool checked)
{
    ui->tableWidget->setAlternatingRowColors(checked);
}

void MainWindow::on_checkBox_HorizontalHeader_clicked(bool checked)
{
    ui->tableWidget->horizontalHeader()->setVisible(checked);
}

void MainWindow::on_checkBox_VerticalHeader_clicked(bool checked)
{
    ui->tableWidget->verticalHeader()->setVisible(checked);
}

void MainWindow::on_radioButton_SelectRow_clicked()
{
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
}

void MainWindow::on_radioButton_SelectItem_clicked()
{
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectItems);
}

void MainWindow::on_tableWidget_currentCellChanged(int currentRow, int currentColumn, int previousRow, int previousColumn)
{
    QTableWidgetItem* item = ui->tableWidget->item(currentRow, currentColumn);
    if (item == nullptr)
        return;

    label_cell_index->setText(QString::asprintf("当前单元格坐标：（%d，%d）", currentRow, currentColumn));
    label_cell_type->setText(QString::asprintf("类型：%d", item->type()));

    item = ui->tableWidget->item(currentRow, column_number::name);
    label_student_id->setText(QString::asprintf("学生学号：%d", item->data(Qt::UserRole).toUInt()));
}



